/**
 * Created by mikezhangding on 21/2/2017.
 */

'use strict';


/*app.controller('donationCtrl', function ($state, $scope, $http, $localStorage,$ionicSlideBoxDelegate) {
 });*/


angular.module('donation.module', [])
  .controller('donationCtrl', function ($scope, $state, $http) {
    $scope.donationList = [];
    $http.get(API("donation/list/1")).success(function (result) {
      $scope.donationList = result.list;
    });

    $scope.goDetailDonate = function (donation) {
      $state.go('tabs.donateView', {id: donation.id});
    }
  })
  .controller('donateViewCtrl', function ($scope, $state, $stateParams, $http, $ionicHistory) {

    $scope.donation = {};
    $http.get(API('donation/') + $stateParams.id).success(function (result) {
      $scope.donation = result;
    });

    //在线募捐
    $scope.selected1 = true;
    $scope.selected2 = false;
    $scope.active1 = true;
    $scope.active2 = false;

    $scope.onTabSelected1 = function () {
      $scope.selected2 = false;
      $scope.selected1 = true;
      $scope.active1 = true;
      $scope.active2 = false;
    };
    $scope.onTabSelected2 = function () {
      $scope.selected1 = false;
      $scope.selected2 = true;
      $scope.active1 = false;
      $scope.active2 = true;
    };

    $scope.datas = [
      {
        name: " 周建国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 100
      },
      {
        name: "黄大国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 50
      },
      {
        name: " 周建国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 100
      },
      {
        name: "黄大国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 50
      },
      {
        name: " 周建国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 100
      },
      {
        name: "黄大国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 50
      },
      {
        name: " 周建国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 100
      },
      {
        name: "黄大国",
        person_pic: "./img/systemImg/person.png",
        date: "2017-3-13 12:34",
        donate: 50
      }

    ];
    $scope.historys=[];
    $http.get(API("donation/history/"+$stateParams.id+"/1")).success(function (result) {
      $scope.historys=result.list;
    })

    /*$scope.logLoadMore = function() {
     $http.get('./js/donation/tsconfig.json').success(function(items) {
     console.log(items);
     $scope.datas=(items);
     $scope.$broadcast('scroll.infiniteScrollComplete');
     });


     };*/

    $scope.goHome = function () {
      $state.go("tabs.home", {}, {reload: false});
      $ionicHistory.clearHistory();
      console.log($ionicHistory.viewHistory())
    };
    $scope.goDonateMoney = function () {
      $state.go("tabs.donateMoney", {id: $stateParams.id});
    }


  })
  .controller('donateMoneyCtrl', function ($scope, $state, $stateParams, $http,$ionicLoading) {
    //捐款支付

    $scope.charge = {}
    $scope.zfbact = false;
    $scope.wxact = false;
    $scope.yzact = false;

    $scope.zfbAct = function () {
      $scope.zfbact = true;
      $scope.wxact = false;
      $scope.yzact = false;

      $scope.charge.payType = 0;
    };
    $scope.wxAct = function () {
      $scope.wxact = true;
      $scope.zfbact = false;
      $scope.yzact = false;
      $scope.charge.payType = 1;
    };
    $scope.yzAct = function () {
      $scope.yzact = true;
      $scope.zfbact = false;
      $scope.wxact = false;
      $scope.charge.payType = 2;
    };

    $scope.zfSubmit = function (valid) {
      // $scope.charge.donationId = $stateParams.id;
      // $http.post(API('donation/pay'), $scope.charge).success(function (result) {
      //   alert("支付成功");
      //   $state.go("tabs.donation");
      // });

      var order={orderType:1,amount:$scope.charge.amount,payType:$scope.charge.payType,targetTableId:$stateParams.id};
      $http.post(API('pay/order'), order).success(function (result) {
        // alert(result);

        var alipayClass = navigator.alipay;
        alipayClass.pay(result,function(resultStatus){
          $ionicLoading.show({
            template:"支付成功",
            noBackdrop: true,
            duration: 2000
          });
        },function(message){
          $ionicLoading.show({
            template:"支付宝支付失败＝" + message,
            noBackdrop: true,
            duration: 2000
          });
        });

      });

    };

  });
